package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.crm.entity.GwdeT;
import cn.e4j.modular.crm.service.impl.GwdeTServiceImpl;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 选项分类设置
 * Created by miaoyl on 2019-2-1.
 */
@Controller
@RequestMapping("/crm/gwdc006")
public class Gwdc006Controller extends BaseController {

    @Autowired
    private GwdeTServiceImpl gwdeTService;

    /**
     * 选项分类设置管理页面
     */
    @RequiresPermissions("crm:gwdc006:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwdc006.html";
    }

    /**
     * 查询选项分类设置信息列表
     */
    @RequestMapping("/getGwdelist")
    @RequiresPermissions("crm:gwdc006:init")
    @ResponseBody
    public ResponseData getGwdelist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdeTService.getGwdelist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询选项分类信息列表
     */
    @RequestMapping("/getGwdeData")
    @ResponseBody
    public ResponseData getGwdeData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdeTService.getGwdeData(inDto);
        return ResponseData.success(result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwdc006:add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdeT> gwdeTList = inDto.getReqList(GwdeT.class);
        Dto outDto = gwdeTService.addData(gwdeTList);
        if(outDto.getAppCode() == 401){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDE1.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDE1.getMessage());
        }else if(outDto.getAppCode() == 402){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDE2.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDE2.getMessage());
        } else if(outDto.getAppCode() == 403){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDE3.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDE3.getMessage());
        }else if(outDto.getAppCode() == 404){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDE4.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDE4.getMessage());
        }
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwdc006:edit")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwdeT> gwdeTList = JSONObject.parseArray(jsonObject.getString("list1"), GwdeT.class);
        Dto outDto = gwdeTService.editData(gwdeTList,jsonObject.getString("GWDE001_old"));
        if(outDto.getAppCode() == 401){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDE1.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDE1.getMessage());
        }else if(outDto.getAppCode() == 402){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDE2.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDE2.getMessage());
        }else if(outDto.getAppCode() == 403){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDE3.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDE3.getMessage());
        }else if(outDto.getAppCode() == 404){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDE4.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDE4.getMessage());
        }
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwdc006:del")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        gwdeTService.delete(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwdc006:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdeT> gwdeTList = inDto.getReqList(GwdeT.class);
        gwdeTService.delDataBatch(gwdeTList);
        return SUCCESS_TIP;
    }

    /**
     * 查询选项分类信息列表
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdeTService.getList(inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询返利编号列表
     */
    @RequestMapping("/getGwde017List")
    @ResponseBody
    public ResponseData getGwde017List(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdeTService.getGwde017List(inDto);
        return ResponseData.success(result);
    }

}
